﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.EventSystems;
using UnityEngine.UI;

public class CustomLayoutView : MonoBehaviour, IPointerClickHandler
{
    public GameObject target;
    ButtonExtension left;
    ButtonExtension right;
    ButtonExtension up;
    ButtonExtension down;
    ButtonExtension big;
    ButtonExtension small;
    Button reset;
    Button save;
    Button back;

    public float MoveSpeed = 30;
    public float scaleSpeed = 0.05f;
    public float minScale = 0.1f;
    public float maxScale = 3f;
    float scale = 1;

    Vector3[] defaultPositon=new Vector3[7];
    Vector3[] defaultSize=new Vector3[7];

    private void Awake()
    {
        //保存默认位置
        for (int i = 0; i < 7; i++)
        {
            defaultPositon[i] = new Vector3(-800 + i * 200, 50, 0);
            defaultSize[i] = new Vector3(1, 1, 1);
        }
    }

    // Start is called before the first frame update
    void Start()
    {
        left = this.transform.Find("Left").GetComponent<ButtonExtension>();
        right = this.transform.Find("Right").GetComponent<ButtonExtension>();
        up = this.transform.Find("Up").GetComponent<ButtonExtension>();
        down = this.transform.Find("Down").GetComponent<ButtonExtension>();
        big = this.transform.Find("Big").GetComponent<ButtonExtension>();
        small = this.transform.Find("Small").GetComponent<ButtonExtension>();
        reset = this.transform.Find("Reset").GetComponent<Button>();
        save = this.transform.Find("Save").GetComponent<Button>();
        back = this.transform.Find("Back").GetComponent<Button>();

        left.onPress.AddListener(LeftOnPress);
        right.onPress.AddListener(RightOnPress);
        up.onPress.AddListener(UpOnPress);
        down.onPress.AddListener(DownOnPress);
        big.onPress.AddListener(BigOnPress);
        small.onPress.AddListener(SmallOnPress);

        reset.onClick.AddListener(ResetOnClick);
        save.onClick.AddListener(SaveOnClick);
        back.onClick.AddListener(BackOnClick);
       
    }
    private void OnEnable()
    {
        //将保存的数据传递给按钮
        for (int i = 0; i < 7; i++)
        {
            KeyData key = GameData.keyDatas[i];
            this.transform.Find("CustomKey" + (i + 1)).localScale = new Vector3(key.r, key.r, 1);
            this.transform.Find("CustomKey" + (i + 1)).localPosition = new Vector3(key.x, key.y, 0);
        }
    }

    void LeftOnPress()
    {
        target.transform.Translate(Vector3.left * MoveSpeed);
    }
    void RightOnPress()
    {
        target.transform.Translate(Vector3.right * MoveSpeed);
    }
    void UpOnPress()
    {
        target.transform.Translate(Vector3.up * MoveSpeed);
    }
    void DownOnPress()
    {
        target.transform.Translate(Vector3.down * MoveSpeed);
    }
    void BigOnPress()
    {
        if (scale <= maxScale)
            scale += scaleSpeed;
        target.transform.localScale = new Vector3(scale, scale, 1);
    }
    void SmallOnPress()
    {
        if (scale >= minScale)
            scale -= scaleSpeed;
        target.transform.localScale = new Vector3(scale, scale, 1);
    }
    void ResetOnClick()
    {
        for (int i = 0; i < 7; i++)
        {
            this.transform.Find("CustomKey" + (i + 1)).transform.localPosition = defaultPositon[i];
            this.transform.Find("CustomKey" + (i + 1)).transform.localScale = defaultSize[i];
        }
    }
    void SaveOnClick()
    {
        for (int i = 0; i < 7; i++)
        {
            KeyData key = new KeyData();
            key.r = this.transform.Find("CustomKey" + (i + 1)).localScale.x;
            key.x = this.transform.Find("CustomKey" + (i + 1)).localPosition.x;
            key.y = this.transform.Find("CustomKey" + (i + 1)).localPosition.y;
            GameData.keyDatas[i] = key;
            print(key.x + " " + key.y + " " + key.r);
        }
        
        this.gameObject.SetActive(false);
        
    }
    void BackOnClick()
    {
        this.gameObject.SetActive(false);
    }

    public void OnPointerClick(PointerEventData eventData)
    {
        if(eventData.pointerEnter.tag=="CustomKey")
        {
            target.GetComponent<Image>().color = new Color(0.8f, 0.8f, 0.8f, 1);
            target = eventData.pointerEnter;
            scale = target.transform.localScale.x;
            target.GetComponent<Image>().color = new Color(1, 1, 1, 1);
        }    
    }
}
